package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.dto.AddressManagementDTO;
import com.woniuxy.entity.dto.PartnerDTO;
import com.woniuxy.mapper.PartnerMapper;
import com.woniuxy.service.PartnerService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class PartnerServiceImpl implements PartnerService {
    @Resource
    private PartnerMapper partnerMapper;
    @Override
    public ResponseData<?> queryAllPartner(PageData<PartnerDTO> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<PartnerDTO> partnerDTOS = partnerMapper.queryAllPartner(pageData.getParam());
        PageInfo pageInfo = new PageInfo(partnerDTOS);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> addPartner(PartnerDTO partnerDTO) {
        partnerMapper.addPartner(partnerDTO);
        return  new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updatePartner(PartnerDTO partnerDTO) {
        if (partnerDTO.getContracted() == 0){
            partnerDTO.setStatus(0);
        }
        partnerMapper.updatePartner(partnerDTO);
        return  new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateStatus(PartnerDTO partnerDTO) {
        if (partnerDTO.getStatus() == 0){
            //启用  修改成1
            if (partnerDTO.getContracted() == 0 ){
                return new ResponseData<>().fail(ResponseEnum.FAIL);
            }
            partnerDTO.setStatus(1);
            partnerMapper.updateStatus(partnerDTO);
            return  new ResponseData<>().ok();
        }else if (partnerDTO.getStatus() == 1){
            //停用  修改成0
            partnerDTO.setStatus(0);
            partnerMapper.updateStatus(partnerDTO);
            return  new ResponseData<>().ok();
        }

        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }

    @Override
    public ResponseData<?> batchUpdateStatus1(List<PartnerDTO> partnerDTOS) {
        List<Integer> ids = new ArrayList<>();
        for (PartnerDTO partnerDTO : partnerDTOS) {
            if (partnerDTO.getContracted() == 0){
                return new ResponseData<>().fail(ResponseEnum.FAIL);
            }
            ids.add(partnerDTO.getId());
        }
        partnerMapper.batchUpdateStatus(ids,"1");
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> batchUpdateStatus0(List<PartnerDTO> partnerDTOS) {
        List<Integer> ids = new ArrayList<>();
        for (PartnerDTO partnerDTO : partnerDTOS) {
            ids.add(partnerDTO.getId());
        }
        partnerMapper.batchUpdateStatus(ids,"0");
        return new ResponseData<>().ok();
    }
}
